import 'dart:developer';
import 'package:flutter/services.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:huxiaodui/api/model/alarm_model.dart';

class NotificationTool {
  /// 单例模式
  static final NotificationTool _instance = NotificationTool._internal();

  factory NotificationTool() {
    return _instance;
  }

  static FlutterLocalNotificationsPlugin notificationsPlugin =
      FlutterLocalNotificationsPlugin();

  static final Map<int, Function(NotificationResponse)?>
      _onDidReceiveBackgroundNotificationResponses = {};
  static final Map<int, Function(NotificationResponse)?>
      _onDidReceiveNotificationResponses = {};

  static void _onDidReceiveBackgroundNotificationResponse(
      NotificationResponse res) {
    Function? fun = _onDidReceiveBackgroundNotificationResponses[res.id];
    log(res.id.toString());
    log(fun.toString());
    if (fun != null) fun(res);
  }

  static void _onDidReceiveNotificationResponse(NotificationResponse res) {
    Function? fun = _onDidReceiveNotificationResponses[res.id];
    log(res.id.toString());
    log(fun.toString());
    if (fun != null) fun(res);
  }

  NotificationTool._internal() {
    notificationsPlugin.initialize(
      const InitializationSettings(
          android: AndroidInitializationSettings('@mipmap/ic_launcher'),
          iOS: DarwinInitializationSettings()),
      onDidReceiveBackgroundNotificationResponse:
          _onDidReceiveBackgroundNotificationResponse,
      onDidReceiveNotificationResponse: _onDidReceiveNotificationResponse,
    );
  }

//  显示通知
  Future<void> alarm(AlarmModel alarm) async {
    FlutterLocalNotificationsPlugin().cancel(1001);
    HapticFeedback.heavyImpact();
    await FlutterLocalNotificationsPlugin().show(
      1001,
      "联动",
      alarm.unit.name,
      const NotificationDetails(
        android: AndroidNotificationDetails("alarm", "联动通知",
            importance: Importance.max,
            priority: Priority.max,
            playSound: false),
        iOS: DarwinNotificationDetails(
          presentAlert: true,
          presentBadge: true,
          presentSound: false,
          presentBanner: true,
          categoryIdentifier: "alarm", // 通知分类
        ),
      ),
    );
  }

  Future<void> notice(
      {String? title,
      String? body,
      Function(NotificationResponse)? onDidReceiveNotificationResponse,
      Function(NotificationResponse)?
          onDidReceiveBackgroundNotificationResponse}) async {
    _onDidReceiveBackgroundNotificationResponses
        .addAll({3: onDidReceiveBackgroundNotificationResponse});
    _onDidReceiveNotificationResponses
        .addAll({3: onDidReceiveNotificationResponse});
    FlutterLocalNotificationsPlugin().show(
      3,
      title,
      body,
      const NotificationDetails(
        android: AndroidNotificationDetails(
          "notice",
          "普通通知",
          importance: Importance.high,
          priority: Priority.high,
        ),
        iOS: DarwinNotificationDetails(
          presentAlert: true,
          presentBadge: true,
          presentSound: true,
          presentBanner: true,
          categoryIdentifier: "notice", // 通知分类
        ),
      ),
    );
  }

  Future<void> warn({String? title, String? body}) async {
    FlutterLocalNotificationsPlugin().show(
      6,
      title,
      body,
      const NotificationDetails(
        android: AndroidNotificationDetails(
          "warn",
          "异常通知",
          importance: Importance.min,
          priority: Priority.min,
          playSound: false,
        ),
        iOS: DarwinNotificationDetails(
          presentAlert: false,
          presentBadge: false,
          presentSound: false,
          presentBanner: false,
          categoryIdentifier: "notice", // 通知分类
        ),
      ),
    );
  }

  Future<void> run() async {
    FlutterLocalNotificationsPlugin().show(
      200,
      '警保联动',
      '运行中...',
      const NotificationDetails(
        android: AndroidNotificationDetails(
          'run',
          '后台运行',
          playSound: false,
          // ongoing: true,
          importance: Importance.min,
          priority: Priority.min,
        ),
      ),
    );
  }

  Future<void> downApk(String title, String body,
      {Function(NotificationResponse)? onDidReceiveNotificationResponse,
      Function(NotificationResponse)?
          onDidReceiveBackgroundNotificationResponse}) async {
    _onDidReceiveBackgroundNotificationResponses
        .addAll({4: onDidReceiveBackgroundNotificationResponse});
    _onDidReceiveNotificationResponses
        .addAll({4: onDidReceiveNotificationResponse});
    FlutterLocalNotificationsPlugin().show(
      4,
      title,
      body,
      const NotificationDetails(
        android: AndroidNotificationDetails(
          "downApk",
          "下载通知",
          importance: Importance.low,
          priority: Priority.low,
        ),
        iOS: DarwinNotificationDetails(
          presentAlert: true,
          presentBadge: true,
          presentSound: true,
          presentBanner: true,
          categoryIdentifier: "downApk", // 通知分类
        ),
      ),
    );
  }

  Future<void> show({String? title, String? body}) async {
    FlutterLocalNotificationsPlugin().show(
      5,
      title,
      body,
      const NotificationDetails(
        android: AndroidNotificationDetails(
          'show',
          '通知',
          ongoing: true,
        ),
      ),
    );
  }
}
